﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace AVCUMPlatform.HelperLibrary
{
    /// <summary>
    /// 对象属性复制工具列
    /// </summary>
    public class MapperHelper
    {
        /// <summary>
        /// 将source对象的属性（同名、同类型），复制到target对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <typeparam name="S">源类型</typeparam>
        /// <param name="target">目标对象</param>
        /// <param name="source">源对象</param>
        /// <returns></returns>
        public static T Mapper<T, S>(T target, S source)
        {
            if (source == null)
            {
                return target;
            }
            var spList = typeof(S).GetProperties();
            var tpList = typeof(T).GetProperties();
            foreach (PropertyInfo sp in spList)
            {
                foreach (PropertyInfo tp in tpList)
                {
                    if (tp.Name == sp.Name && tp.PropertyType.Name == sp.PropertyType.Name)
                    {
                        tp.SetValue(target, sp.GetValue(source));
                    }
                }
            }

            return target;
        }

        /// <summary>
        /// 将source对象的属性（同名、同类型），复制到target对象，并返回新的target对象
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <typeparam name="S">源类型</typeparam>
        /// <param name="source">源对象</param>
        /// <returns></returns>
        public static T Mapper<T, S>(S source)
        {
            if (source == null)
            {
                return default(T);
            }
            T target = Activator.CreateInstance<T>();
            var spList = typeof(S).GetProperties();
            var tpList = typeof(T).GetProperties();
            foreach (PropertyInfo sp in spList)
            {
                foreach (PropertyInfo tp in tpList)
                {
                    if (tp.Name == sp.Name && tp.PropertyType.Name == sp.PropertyType.Name)
                    {
                        tp.SetValue(target, sp.GetValue(source));
                    }
                }
            }

            return target;
        }

        /// <summary>
        /// 将source对象列表的属性（同名、同类型），复制到target对象列表，并返回新的target对象列表
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <typeparam name="S">源类型</typeparam>
        /// <param name="sourceList">源对象列表</param>
        /// <returns></returns>
        public static List<T> MapperList<T, S>(List<S> sourceList)
        {
            if (sourceList == null)
            {
                return default;
            }
            List<T> targetList = new List<T>();
            var spList = typeof(S).GetProperties();
            var tpList = typeof(T).GetProperties();
            foreach (var source in sourceList)
            {
                T target = Activator.CreateInstance<T>();
                foreach (PropertyInfo sp in spList)
                {
                    foreach (PropertyInfo tp in tpList)
                    {
                        if (tp.Name == sp.Name && tp.PropertyType.Name == sp.PropertyType.Name)
                        {
                            tp.SetValue(target, sp.GetValue(source));
                        }
                    }
                }
                targetList.Add(target);
            }

            return targetList;
        }
    }
}
